#### Figure S6: Norm Violations and Violence Support (By Party and Over Time, Continuous) ####

# Libraries
# library(here)
# library(rio)
# library(tidyverse)
# library(ggthemes)
# library(srvyr)

# data_pnas = import(here("Data","data_pnas.rds"))
# source(here("Code","Functions","funcs_theme.R"))

norm_ind_c = paste0("norm_",c("judges","polling","executive","censorship","loyalty"),"c")

cs_summary_c = data_pnas |> 
  filter(pid %in% c("Democrat","Republican")) |> # Just D/R
  select(pid, all_of(c(norm_ind_c)),
         uid, weight) |> 
  pivot_longer(-c("pid","uid","weight"), names_to = "var", values_to = "val") |> 
  as_survey_design(ids = uid, weights = weight) |> 
  group_by(pid, var) |> 
  summarise(val_sum = survey_mean(val, na.rm = T, vartype = "ci")) |> 
  ungroup()  |> 
  mutate(var_typ = ifelse(str_detect(var,"norm"), "Democratic Norms", "Violence"),
         var = recode_factor(var,
                             norm_pollingc = "Reduce outparty polling stations",
                             norm_loyaltyc = "More loyal to party than Constitution",
                             norm_judgesc = "Ignore outparty court decisions",
                             norm_executivec = "President should circumvent Congress",
                             norm_censorshipc = "Censor partisan media")
  )

fig_s6_a = cs_summary_c |> 
  ggplot(aes(x = val_sum, 
             y = tidytext::reorder_within(var, val_sum, var_typ),
             group = pid,
             label = round(val_sum,2))) +
  geom_pointrange(aes(xmin = val_sum_low,
                      xmax = val_sum_upp,
                      color = pid),
                  position = position_dodge(.75), size=.4) +
  geom_text(aes(x = val_sum + .35),
            position = position_dodge(.75), size=3) +
  force_panelsizes(rows = c( 1,.75)) +
  scale_color_manual(name = "Party",
                     values = c("blue","red")) +
  scale_x_continuous(limits = c(1,5)) +
  tidytext::scale_y_reordered() +
  labs(x = "Average Support (1-5)",
       y = NULL) +
  theme_prl() +  
  theme( strip.text.x = element_text(size = 8),
         panel.spacing.y = unit(0, "lines"),
         legend.position = "none")

ot_norms_c = data_pnas |> 
  filter(pid %in% c("Democrat","Republican")) |> 
  mutate(week = floor_date(date-2, unit = "week"),# - 2 so weeks start on Friday (survey start)
         weight = weight * wt_adj) |> 
  select(pid, week, all_of(norm_ind_c), weight) |> 
  pivot_longer(all_of(norm_ind_c), names_to = "var", values_to = "val") |> 
  mutate(var = recode_factor(var,
                             norm_pollingc = "Reduce outparty\npolling stations",
                             norm_loyaltyc = "More loyal to party\nthan Constitution",
                             norm_judgesc = "Ignore outparty\ncourt decisions",
                             norm_executivec = "President should\ncircumvent congress",
                             norm_censorshipc = "Censor partisan media"))

fig_s6_b = ot_norms_c |> 
  ggplot(aes(x = week, y = val, group = pid, color = pid)) +
  geom_smooth(weight = "weight") +
  facet_wrap(~ var, ncol = 2) +
  scale_x_date(date_labels = "%b %Y",
               date_breaks = "3 month") +
  scale_y_continuous(breaks = c(1,3,5),
                     limits = c(1,5)) +
  scale_color_manual(name = "Party",
                     values = c("blue","red")) +
  labs(x = NULL, y = "Average Estimated Support") +
  theme_prl() +
  theme(axis.text.x = element_text(angle = 0, hjust = 1, vjust = 1, size = 8)) +
  guides(color = guide_legend(override.aes = list(fill = NA))) +
  theme(legend.position = c(0.9, 0.1), legend.direction="vertical",legend.key = element_rect(fill = "white"))

fig_s6 = cowplot::plot_grid(fig_s6_a, fig_s6_b, ncol = 1, rel_heights = c(.9,.9), labels=c("A.","B."))

print(fig_s6)

ggsave(here("Plots","Supplementary","figure_s6.png"),
       fig_s6,
       units = "in",
       width = 5, height = 7,
       dpi = 600)